package com.ddzh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddzh.dto.RoleDto;
import com.ddzh.dto.UserDto;
import com.ddzh.dto.UserRoleDto;
import com.ddzh.entity.PageReq;
import com.ddzh.entity.PageRes;
import com.ddzh.mapper.RoleMapper;
import com.ddzh.pojo.Role;
import com.ddzh.pojo.RoleMenu;
import com.ddzh.pojo.User;
import com.ddzh.pojo.UserRole;
import com.ddzh.service.*;
import com.ddzh.vo.RoleVo;
import com.ddzh.vo.UserVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @Override
    public List<Role> findByUserId(Long userId) {
        return this.baseMapper.findByUserId(userId);
    }

    @Transactional
    @Override
    public boolean saveOrUpdateRole(RoleDto roleDto) {
        //用户账户唯一
        //判断当前是添加还是修改
        if(roleDto.getRole().getId()==null){// 添加
            //判断数据库中是否有相同账户名的用户
            if (this.baseMapper.findByRoleName(roleDto.getRole().getRoleName()) !=null ){
                return false;
            }
            if (this.baseMapper.findByRoleCode(roleDto.getRole().getRoleCode()) !=null ){
                return false;
            }
        }
        //保存用户数据
        this.saveOrUpdate(roleDto.getRole());
        //维护 用户角色 中间表数据
        //先清理掉之前维护的关系
        userRoleService.deleteByRoleId(roleDto.getRole().getId());
        roleMenuService.deleteByRoleId(roleDto.getRole().getId());
        //重新插入数据
        List<UserRole> userList = new ArrayList<>();
        List<RoleMenu> menuList = new ArrayList<>();
        if (roleDto.getUserIds()!=null) {
            for (Long userId : roleDto.getUserIds()) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleDto.getRole().getId());
                userRole.setUserId(userId);
                userList.add(userRole);
            }
            userRoleService.saveBatch(userList);
        }

        if (roleDto.getMenuIds()!=null) {
            for (Long menuId : roleDto.getMenuIds()) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleDto.getRole().getId());
                roleMenu.setMenuId(menuId);
                menuList.add(roleMenu);
            }
            roleMenuService.saveBatch(menuList);
        }
        return true;
    }

    @Override
    public List<RoleVo> findAll(PageReq pageReq) {

        ArrayList<RoleVo> roleVoList = new ArrayList<>();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();


        if (pageReq.getQueryString()!=null && "".equals(pageReq.getQueryString())==false){
            queryWrapper.and(wapper->{
                wapper.like("role_name",pageReq.getQueryString()).or()
                        .like("role_code",pageReq.getQueryString());
            });
        }
        queryWrapper.orderByDesc("id");

        List<Role> list = list(queryWrapper);
        for (Role role : list) {
            //查询每一个 角色 的 用户名=>直接返回集合
            List<String> userNameList = userService.findUserNameByURoleId(role.getId());
            List<String> menuPathList = menuService.findMenuPathByURoleId(role.getId());

            //将 角色 数据 和 用户名 存在集合中
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleName(role.getRoleName());
            roleVo.setRoleCode(role.getRoleCode());
            roleVo.setUserNameList(userNameList);
            roleVo.setMenuPathList(menuPathList);
            roleVo.setId(role.getId());
            //TODO 菜单路径数据没取
            roleVoList.add(roleVo);
        }
        return roleVoList;
    }

    @Override
    public List<String> findRoleNameByUserId(Long userId) {
        return this.baseMapper.findRoleNameByUserId(userId);
    }
//    @Override
//    public PageRes<Role> findByPage(PageReq pageReq) {
//        //分页查询时将用户角色信息一并查出来
//
//        IPage<Role> iPage = new Page<>();
//        iPage.setSize(pageReq.getPageSize());
//        iPage.setCurrent(pageReq.getCurrentPage());
//        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_delete",0);
//        if (pageReq.getQueryString() !=null && "".equals(pageReq.getQueryString().trim())==false) {
//            queryWrapper.and(wrapper->{
//                wrapper.like("role_name",pageReq.getQueryString()).or()
//                        .like("role_code",pageReq.getQueryString());
//            });
//        }
//        queryWrapper.orderByDesc("id");
//        IPage<Role> page = this.page(iPage, queryWrapper);
//        PageRes<Role> pageRes = new PageRes<>();
//        pageRes.setList(page.getRecords());
//        pageRes.setTotal(page.getTotal());
//        return pageRes;

//    }
//    @Override
//    public boolean deleteById(Long id) {
//        //删除之前要判断当前用户是否正在被角色使用
//        List<Role> roleList = roleService.findByUserId(id);
//        if (roleList.size()>0){//用户正在被角色使用
//            return false;
//        }
//        User user = new User();
//        user.setId(id);
//        user.setIs_delete(1);
//
//        return this.updateById(user);

//    }
//    @Override
//    public List<User> getDetail(Long groupId) {
//        return null;

//    }
//    @Override
//    public List<Role> findAll() {
//        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_delete" ,0);
//        return list(queryWrapper);
//    }
//
}
